<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Notepad</title>

    <!-- bootstrap -->
    <link rel="stylesheet" href="./assets/css/bootstrap.min.css" />

    <!-- animate.css -->
    <link rel="stylesheet" href="./assets/css/animate.min.css" />

    <!-- mui框架css -->
    <link rel="stylesheet" href="./assets/plugins/mui/css/mui.min.css">

    <!-- 城市组件 -->
    <link rel="stylesheet" href="./assets/plugins/city-picker/css/city-picker.css">

    <!-- 重置样式 -->
    <link rel="stylesheet" href="./assets/css/reset.css" />


    <!-- 公共样式 -->
    <link rel="stylesheet" href="./assets/css/common.css" />

    <!-- 页面独立的 -->
    <link rel="stylesheet" href="./assets/css/UserProfile.css" />
</head>

<body>
    <!-- 用户信息 -->
    <div class="user">
        <div class="avatar wow fadeInUp">
            <img src="./assets/images/avatar.png" style="width: 100%; height: 100%; object-fit: cover;" />
        </div>
    </div>

    <form class="profile wow fadeInUp" data-wow-delay="200ms" method="post" enctype="multipart/form-data">
        <div class="form-group">
            <label for="nickname">昵称</label>
            <input type="text" class="form-control" id="nickname" name="nickname" placeholder="请输入姓名" required />
        </div>

        <div class="form-group">
            <label for="mobile">手机号码</label>
            <input type="text" class="form-control" id="mobile" name="mobile" placeholder="请输入手机号码" required />
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码" required />
        </div>

        <div class="form-group">
            <label for="gender">性别</label>
            <select class="form-control" name="gender">
                <option>请选择</option>
                <option value="0">保密</option>
                <option value="1">男</option>
                <option value="2">女</option>
            </select>
        </div>

        <div class="form-group">
            <label for="password">城市</label>
            <div class="control-region" style="position: relative;">
                <input id="city" class="form-control" data-toggle="city-picker" name="region" type="text" />
            </div>
        </div>

        <div class="form-group">
            <label for="address">详细地址</label>
            <input type="text" class="form-control" id="address" placeholder="请输入详细地址" name="address" />
        </div>

        <div class="form-group" style="display: none;">
            <label for="address">头像</label>
            <input type="file" class="form-control" id="avatar" name="avatar" />
        </div>

        <div class="action">
            <button class="btn" type="submit">提交</button>
        </div>
    </form>
</body>

</html>
<!-- jquery  -->
<script src="./assets/js/jquery.min.js"></script>

<!-- bootstrap -->
<script src="./assets/js/bootstrap.min.js"></script>

<!-- wow.js -->
<script src="./assets/js/wow.min.js"></script>

<!-- mui -->
<script src="./assets/plugins/mui/js/mui.min.js"></script>

<!-- 城市组件 -->
<script src="./assets/plugins/city-picker/js/city-picker.data.min.js"></script>
<script src="./assets/plugins/city-picker/js/city-picker.min.js"></script>

<!-- 请求插件 -->
<script src="./assets/plugins/axios/axios.min.js"></script>
<script src="./assets/plugins/axios/request.js"></script>

<!-- 引入cookie插件 -->
<script src="./assets/plugins/cookle.js"></script>
<!-- 校验用户是否登录 -->
<script src="./assets/plugins/check.js"></script>


<script>
    //实例化wow.js
    new WOW().init()

    let Business = cookie.get('Business') ? JSON.parse(cookie.get('Business')) : {}
    if (Business.avatar) {
        $('#avatar img').attr('src', Business.avatar)
        $('#nickname').val(Business.nickname)
        $('#mobile').val(Business.mobile)
        $('#gender').val(Business.gender)
        $('address').val(Business.address)


        // 地区回显
        $('#city').citypicker({
            province: Business.province,
            city: Business.city,
            district: Business.district
        });
    }


    // 头像
    let avatar = null

    //头像预览
    $('.avatar img').click(function () {
        $('#avatar').click()
    })

    $('#avatar').change(function (e) {
        avatar = e.target.files[0]
        let src = URL.createObjectURL(avatar);
        $('.avatar img').attr('src', src)
    })

    $('form').submit(async function (e) {
        e.preventDefault();


        //序列化表格元素（类似‘serialize()’ 方法)返回json数据结构数据
        var formdata = $(this).serializeArray();
        let obj = {}
        $.each(formdata, function (i, item) {
            obj[item.name] = item.value
        });

        let data = {
            id: Business.id,
            nickname: obj.nickname,
            mobile: obj.mobile,
            gender: obj.gender,
            address: obj.address
        }

        // 判断是否修改密码
        if (obj.password) {
            data.password = obj.password
        }

        // 判断是否修改头像
        if (avatar) {
            data.avatar = avatar;
        }

        // 判断是否修改地区
        var citypicker = $('#city').data('citypicker');
        var code = citypicker.getCode('district') || citypicker.getCode('city') || citypicker.getCode('province');
        if (code) {
            data.region = code;
        }

        let result = await UPLOAD({
            url: 'business/profile', params: data
        })

        if (result.code == 1) {
            mui.toast(result.msg);
            // 存储用户信息
            cookie.set('Business', JSON.stringify(result.data))
            setTimeout(() => {
                location.href = 'my.html'
            }, 500)
        } else {
            mui.toast(result.msg)
        }
    })
</script>